<?php
defined('IN_TS') or die('Access Denied.');

$userid = aac('user')->isLogin();

//判断发布者状态
if(aac('user')->isPublisher()==false) tsNotice('不好意思，你还没有权限发布内容！');

switch($ts){

	case "":
		$arrOptions = $new['wish']->findAll('wish_options');
		foreach($arrOptions as $item){
			$strOption[$item['optionname']] = stripslashes($item['optionvalue']);
		}
		$arrGroup = $new['wish']->findAll('group','isplace>0','groupid asc');
		if ($strOption['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
//        if ($TS_APP['allowpost'] == 0 && $TS_USER['isadmin'] == 0) {
            tsNotice('系统设置不允许会员发心愿！');
        }

		$strTopic = aac('article')->find('article',array(
			'articleid'=>713,
		));
		$strTopic['content'] = tsDecode($strTopic['content'],$tp);

		$title = '发布心愿';
		include template("add");
	
		break;
	
	case "do":
	
		$title = trim($_POST['title']);
		$typeid = intval($_POST['typeid']);
		$limittime = trim($_POST['limittime']);
		$quota = intval($_POST['quota']);
		$link = trim($_POST['link']);
		$wisher = trim($_POST['wisher']);
		$contact = trim($_POST['contact']);
		$phone = trim($_POST['phone']);
		$grade = intval($_POST['grade']);
		$class = intval($_POST['class']);
		$groupid = intval($_POST['groupid']);
		$content = tsClean($_POST['content']);
		$photo = trim($_POST['photo']);

		if($photo==''){
			tsNotice('必须上传封面照片！');
		};
		$img_path = $photo;

		// $img_path = getTextPhotos(tsDecode($content));
		// if($img_path==array()){
		// 	tsNotice('请至少插入一张图片作为封面！');
		// };
		// $count = count($img_path);
		// $img_path = $img_path[$count-1];
		//$url=preg_replace('#/$#','',SITE_URL);
		//$img_path = $url.$img_path;




		$limittime = trim(str_replace('AM','',$limittime));
		$limittime = trim(str_replace('PM','',$limittime));
		$limittime = strtotime($limittime);

		if($limittime==''){
			tsNotice('设一个期限呗');
		}

		if($quota==''){
			tsNotice('数目不能为空');
		}
		if($wisher==''){
			tsNotice('愿主不能为空');
		}
		if($groupid==''){
			tsNotice('分馆不能为空');
		}

		if($title == '' || $content == ''){
			tsNotice('标题和内容不能为空');
		}

        //1审核后显示0不审核
        if ($TS_APP['isaudit'] == 1) {
            $isaudit = 1;
        } else {
            $isaudit = 0;
        }

		$arrGroup = aac('group')->getOneGroup($groupid);

		if($contact==''){
			$contact = $arrGroup['contact'];
		}

		if($phone==''){
			$phone = $arrGroup['phone'];
		}

		
		$wishid = $new['wish']->create('wish',array(
			
			'userid'	=> $userid,
			'title'	=> $title,
			'typeid' => $typeid,
			'limittime' => $limittime,
			'quota'	=> $quota,
			'link' => $link,
			'contact' => $contact,
			'phone' => $phone,
			'wisher' => $wisher,
			'groupid' => $groupid,
			'grade'  => $grade,
			'class'	=> $class,
			'content' => $content,
			'isaudit'=>$isaudit,
			'addtime'	=> time(),
			'path'=> dirname($img_path),
			'photo'=> $img_path,
			'book' => '',
			'image' => '',
			'face' => '',
			'count_userdo' => 0,
			'count_userwish' => 0,
			'count_recommend' => 0,
			'isrecommend' => 0,
		));

		//上传
		// $arrUpload = tsUploadPhotoUrl($img_path,$wishid,'wish',array('jpg','gif','png'));
		// if($arrUpload){
		// 	$new['wish']->update('wish',array(
		// 		'wishid'=>$wishid,
		// 	),array(
		// 		'path'=>$arrUpload['path'],
		// 		'photo'=>$arrUpload['url'],
		// 	));
		// }

		header("Location: ".tsUrl('wish','show',array('id'=>$wishid)));
	
		break;
		
	//地图 
	case "map":
	
		$dd = isset($_GET['dd']) ? $_GET['dd'] : '中国北京';

		include template('add_map');
	
		break;

}